<?php
/*
 * Created on Feb 14, 2008
 *
 * This is an open source software, please obey the GPL
 * The Referee Service is offered by Cacomand Inc. 
 * View Http://www.cacomand.com for support or more information
 * You can also view distrema.googlecode.com to join the community.
 */
 include("lib_dps.php");
 //设置REF的管理员用户名和密码
 	function check_amount($payer,$amount)
 	/*
 	 * 功能描述:确认支付者帐户上是否有这么多活钱
 	 * 返回值:<有>true,<无/失败>false
 	 * 实现:读取ref的cash_pool中的avilable,并于amount比较
 	 */
 	{
 		global $ref_host;
		global $ref_username;
		global $ref_password,$ref_basename;
		global $developer;
		$ref_link=mysql_connect($ref_host,$ref_username,$ref_password);
		mysql_select_db($ref_basename,$ref_link);	
		if ($ref_link!=FALSE)
		{
			$select_format='SELECT available FROM cash_pool WHERE epsn="%s" AND user_ID=%d';
			$query=sprintf($select_format,$payer->epsn,$payer->user_ID);
			if($developer)
				echo("".$query."<br />");
			$res_tocheck=mysql_query($query,$ref_link);
			if($obj_tocheck=mysql_fetch_object($res_tocheck))
			{
				if($obj_tocheck->available>=$amount)
					return true;
			}
		}
		return false;
 	}
 	
 	function freeze_money($payer,$amount)
 	/*
 	 * 功能描述:冻结用户的资金
 	 * 返回值:<成功>true,<失败>false
 	 * 实现:读取ref的cash_pool中的avilable和freezed,做相应的加减后写回
 	 */
 	{
 		global $ref_poweruser,$ref_powerpassword;
 		global $developer,$ref_host,$ref_basename;
		global $ref_link;
		if ($ref_link!=FALSE)
		{
			$select_format='SELECT available,freezed FROM cash_pool WHERE epsn="%s" AND user_ID=%d';
			$query=sprintf($select_format,$payer->epsn,$payer->user_ID);
			if($developer)
				echo("".$query."<br />");
			$res_toedit=mysql_query($query,$ref_link);
			if($obj_toedit=mysql_fetch_object($res_toedit))
			{
				$new_available=$obj_toedit->available-$amount;
				$new_freezed=$obj_toedit->freezed+$amount;
				$update_format='UPDATE cash_pool SET available=%d,freezed=%d WHERE epsn="%s" AND user_ID=%d';
				$query=sprintf($update_format,$new_available,$new_freezed,$payer->epsn,$payer->user_ID);
				if($developer)
					echo("".$query."<br />");
				$res_nouse=mysql_query($query,$ref_link);
				return true;
			}
		}
		return false;
 	}
 class payer extends dps_user
 {
 	public $realname;
 	function payer($epsn,$user_ID)
 	{
 		parent::dps_user($epsn,$user_ID);
 		$tmp=$this->smart_get(true,23,"姓名");
 		$this->realname=$tmp[0]["memo"];
 	}
 	function intend_to_pay($receiver,$amount,$memo,$receivable)
 	/*
 	 * 功能描述:创建一个付款意向,并冻结相关款项
 	 * 返回值:<成功>true,<失败>false
 	 * 实现:0.判断用户是否存在;1.判断payer的帐户上是否有足够的活钱,没有的话返回false;2.给receiver发送收款信息,不成功的话返回false;3.从payer的活钱上减去$amount,冻结钱上加上$amount;
 	 */
 	{
 		if(!check_amount($this,$amount))
 			return false;
 		if(!freeze_money($this,$amount))
 			return false;
 		if(!send_data($this,$receiver,25,date("Y/m/d/G/i/s").".".$amount.".".$this->epsn.".".$this->user_ID.".".$receiver->epsn.".".$receiver->user_ID,"未收"))
 			return false;
 		if($receivable)
 		{
 			if($this->add_record(26,date("Y/m/d/G/i/s").".".$amount.".".$this->epsn.".".$this->user_ID.".".$receiver->epsn.".".$receiver->user_ID,"可收") AND $this->add_record(27,date("Y/m/d/G/i/s").".".$amount.".".$this->epsn.".".$this->user_ID.".".$receiver->epsn.".".$receiver->user_ID,$memo))
 				return true;
 		}
 		else
 		{
 			if($this->add_record(26,date("Y/m/d/G/i/s").".".$amount.".".$this->epsn.".".$this->user_ID.".".$receiver->epsn.".".$receiver->user_ID,"不可收") AND $this->add_record(27,date("Y/m/d/G/i/s").".".$amount.".".$this->epsn.".".$this->user_ID.".".$receiver->epsn.".".$receiver->user_ID,$memo))
 				return true;
 		}
 		return false;
 	}
 	
 	function enable_receive($payment)
 	{
 		if($this->smart_get(false,26,$payment->pay_info))
 		{
 			$this->reset_record(26,$payment->pay_info,"可收");
 			return true;
 		}
 		return false;
 	}
 }
 
 class receiver extends dps_user
 {
 	public $realname;
 	function receiver($epsn,$user_ID)
 	{
 		global $developer;
		parent::dps_user($epsn,$user_ID);
 		$tmp=$this->smart_get(true,23,"姓名");
 		$this->realname=$tmp[0]["memo"];
		if($developer)
			echo("建立了收款者对象<br />");
 	}
 	
 	function get_pays_to_me()
 	{
 		global $receive_pay_processor;
 		$n=0;
		if($pays=$this->get_var_from_sent_data(25,"","未收"))
		{
	 		foreach($pays as $pay)
	 		{
	 			list($pay_time,$amount,$payer_epsn,$payer_user_ID,$receiver_epsn,$receiver_user_ID)=explode(".",$pay["caption"]);
	 			$payer=new payer($payer_epsn,$payer_user_ID);
	 			if($rcv=$payer->get_var_from_ccr(26,$pay["caption"],"可收") AND $rcm=$payer->get_var_from_ccr(27,$pay["caption"]))
	 			{
	 				$atr[$n]["pay_memo"]=$rcm[0]["memo"];
	 				$atr[$n]["payer_name"]=$payer->realname;
					$atr[$n]["amount"]=$amount;
					$atr[$n]["pay_info"]=$pay["caption"];
					$n++;
	 			}
	 		}
		}
		return $atr;
 	}
 	
 	function receive($payment)
 	{
 		global $developer;
		if($developer)
		{
			echo("收款：".$payment->pay_info."<br />");
			echo($payment->receiver->epsn.";".$payment->receiver->user_ID.";".$payment->receivable.";".$payment->received);
		}
		if($payment->receiver->epsn==$this->epsn AND $payment->receiver->user_ID==$this->user_ID AND $payment->receivable==true AND $payment->received==false)
 		{
 			global $ref_poweruser,$ref_powerpassword;
	 		global $developer,$ref_host,$ref_basename;
			global $ref_link;
			echo("haha");
			if ($ref_link!=FALSE)
			{
				$select_format='SELECT freezed FROM cash_pool WHERE epsn="%s" AND user_ID=%d';
				$query=sprintf($select_format,$payment->payer->epsn,$payment->payer->user_ID);
				if($developer)
					echo("".$query."<br />");
				$res_toedit=mysql_query($query,$ref_link);
				if($obj_toedit=mysql_fetch_object($res_toedit))
				{
					if($obj_toedit->freezed<$payment->amount)
						return false;
					$new_freezed=$obj_toedit->freezed-$payment->amount;
					$update_format='UPDATE cash_pool SET freezed=%d WHERE epsn="%s" AND user_ID=%d';
					$query=sprintf($update_format,$new_freezed,$payment->payer->epsn,$payment->payer->user_ID);
					if($developer)
						echo("".$query."<br />");
					$res_nouse=mysql_query($query,$ref_link);
				}
				
				$select_format='SELECT available FROM cash_pool WHERE epsn="%s" AND user_ID=%d';
				$query=sprintf($select_format,$payment->receiver->epsn,$payment->receiver->user_ID);
				if($developer)
					echo("".$query."<br />");
				$res_toedit=mysql_query($query,$ref_link);
				if($obj_toedit=mysql_fetch_object($res_toedit))
				{
					$new_available=$obj_toedit->available+$payment->amount;
					$update_format='UPDATE cash_pool SET available=%d WHERE epsn="%s" AND user_ID=%d';
					$query=sprintf($update_format,$new_available,$payment->receiver->epsn,$payment->receiver->user_ID);
					if($developer)
						echo("".$query."<br />");
					$res_nouse=mysql_query($query,$ref_link);
				}
				else
				{
					$insert_format='INSERT INTO cash_pool (epsn,user_ID,available,freezed) VALUE ("%s",%d,%d,%d)';
					$query=sprintf($insert_format,$payment->receiver->epsn,$payment->receiver->user_ID,$payment->amount,0);
					if($developer)
						echo("".$query."<br />");
					$res_nouse=mysql_query($query,$ref_link);
				}
			}
 			$this->del_record(25,$payment->pay_info);
 			$this->add_record(25,$payment->pay_info,"已收");
 			return true;
 		}
 		return false;
 	}
 }
 
 class payment
 {
 	public $pay_info,$create_time,$amount,$payer_epsn,$payer_user_ID,$receiver_epsn,$receiver_user_ID,$receivable,$received;
 	public $payer,$receiver;
 	function payment($pay_info)
 	{
 		$this->pay_info=$pay_info;
 		list($this->create_time,$this->amount,$this->payer_epsn,$this->payer_user_ID,$this->receiver_epsn,$this->receiver_user_ID)=explode(".",$pay_info);
 		$this->payer=new payer($this->payer_epsn,$this->payer_user_ID);
 		$this->receiver=new receiver($this->receiver_epsn,$this->receiver_user_ID);
 		if(!$tmp=$this->payer->smart_get(false,26,$pay_info))
 			return false;
 		if($tmp[0]["memo"]=="可收")
 			$this->receivable=true;
 		else
 			$this->receivable=false;
 			
 		if(!$tmp=$this->receiver->smart_get(false,25,$pay_info))
 			return false;
 		if($tmp[0]["memo"]=="已收")
		{
 			$this->received=true;
			if($developer)
				echo('该款项已收');
 		}
		else
		{
 			$this->received=false;
			if($developer)
				echo('该款项未收');
 		}
	}
 }
?>
